﻿using System;
using System.Web.Mvc;
using RealtReview.DataAccess.SearchParameters;
using RealtyReview.Business.Interfaces;
using RealtyReview.Business.Models.ViewModels;

namespace RealtyReview.Controllers
{
    public class ReviewController : Controller
    {
        private IReviewsService reviewService;
        public ReviewController(IReviewsService reviewService)
        {
            this.reviewService = reviewService;
        }

        public ActionResult WriteReview()
        {
            return View("Write");
        }

        [HttpPost]
        public ActionResult WriteReview(ReviewDetailsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var reviewId = reviewService.AddReview(Guid.NewGuid(), model);
                var reviewDetails = reviewService.GetReviewDetails(reviewId);
                return View("Details", reviewDetails);
            }
            return View("Write");
        }

        public ActionResult ReadReviews()
        {
            return View("Read");
        }

        public JsonResult SearchReviews()
        {
            var parameters = new ReviewSearchParameters();
            var items = reviewService.GetReviews(parameters);
            return Json(items, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Details(Guid reviewId)
        {
            var reviewDetails = reviewService.GetReviewDetails(reviewId);
            return View("Details", reviewDetails);
        }
    }
}